Ext.onReady(function() {
    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
    var cataname="";                  //这两个变量用来判断页面上的文本是否改变
    var catano="";
    var viewport = new Ext.Viewport({
        layout : 'border',
        items:[{
            tbar : [ new Ext.Toolbar.TextItem(), new Ext.Toolbar.SplitButton({
                text : '新增',
                handler : function() {
                    var father = Ext.getCmp("parent_id").getValue();
                    Ext.getCmp("cata_id").setValue("");
                    Ext.getCmp("cata_name").setValue("");
                    Ext.getCmp("cata_no").setValue("");
                    Ext.getCmp("parent_id").setValue(father);
                },
                iconCls : 'blist',
                menu : {
                    items : [ {
                        text : '同级分类',
                        handler : function() {
                            cataname="";
                            catano="";
                            var father = Ext.getCmp("parent_id").getValue();
                            Ext.getCmp("cata_id").setValue("");
                            Ext.getCmp("cata_name").setValue("");
                            Ext.getCmp("cata_no").setValue("");
                            Ext.getCmp("parent_id").setValue(father);

                        }
                    }, {
                        text : '子级分类',
                        handler : function() {
                            cataname="";
                            catano="";
                            var father = Ext.getCmp("cata_id").getValue();
                            Ext.getCmp("cata_id").setValue("");
                            Ext.getCmp("cata_name").setValue("");
                            Ext.getCmp("cata_no").setValue("");
                            Ext.getCmp("parent_id").setValue(father);

                        }
                    } ]
                }
            }), '-', {
                text : '保存',
                handler : function() {
                    save_button()
                }
            }, '-', {
                xtype:'button',
                text : '删除',
                id:'delete_catalog_button',
                handler : function() {
                    delete_check()
                }
            }

            ],
            region : 'center',
            deferredRender : false,
            activeTab : 0,
            //   frame : true,
            layout : 'border',
            items:[{
                tools : [{
		            id : 'refresh',
		            handler : function() {
		                if(tree_root!=null){
		                    tree_root.reload();
		                }
		
		            }
		        }],     
                region : 'west',
                //id : 'catalog_west-panel',
                title : '指标分类',
                width : 200,
                split:true,
                collapsible : true,
                //  frame : true,
                html : '<div id="catalog_tree_div"></div>'
            //   items:[tree]
            },  {
                region : 'center',
                deferredRender : false,
                activeTab : 0,
                frame : true,
                html : "<div id='panelDiv'></div>"
            }
            ]
        }]

    })
    
    function save_button(){
        CATANAME = Ext.getCmp("cata_name").getValue();
        CATANO=Ext.getCmp("cata_no").getValue();
                    
        if(CATANAME==""||CATANO==""){
            Ext.MessageBox.alert("提示","请输入数据！");
            return;
        }
        if(Ext.getCmp("cata_id").getValue()==""){
            check();
        }
        else{
            checkid();	
        }
    }
    
    function delete_check(){
    	
        var cataid=Ext.getCmp("cata_id").getValue();
        var saveItemInfoUrl = "";
        saveItemInfoUrl = '?className=cn.com.jsepc.rep.catalog.Catalog_Check';
        saveItemInfoUrl += '&methodName=delete_check';
        saveItemInfoUrl += '&paramCount=0';
        saveItemInfoUrl = basePath + 'JarpListener' + saveItemInfoUrl;
        httpR_itemInfo = getXMLHttpRequest();
        httpR_itemInfo.onreadystatechange = return_delete_checked;
        httpR_itemInfo.open("POST", saveItemInfoUrl, true);
        httpR_itemInfo.send(cataid);
    }
    
    function return_delete_checked() {
        if (httpR_itemInfo.readyState == 4) {
            if (httpR_itemInfo.status == 200) {
                ret = httpR_itemInfo.responseText;
                if (ret == 'ok') {
                    check_report();
                } 
                else{
                    Ext.MessageBox.alert("删除失败","请先删除子级")

                }
            }
        }
    }
    
    function check(){
        var cata=Ext.getCmp("cata_name").getValue();
        var formStore = new Ext.data.Store(['CATANAME','CATANO']);
        formStore.CATANAME = Ext.getCmp("cata_name").getValue();
        formStore.CATANO = Ext.getCmp("cata_no").getValue();
        var saveItemInfoUrl = "";
        saveItemInfoUrl = '?className=cn.com.jsepc.rep.catalog.Catalog_Check';
        saveItemInfoUrl += '&methodName=check';
        saveItemInfoUrl += '&paramCount=0';
        saveItemInfoUrl = basePath + 'JarpListener' + saveItemInfoUrl;
        httpR_itemInfo = getXMLHttpRequest();
        httpR_itemInfo.onreadystatechange = returnchecked;
        httpR_itemInfo.open("POST", saveItemInfoUrl, true);
        httpR_itemInfo.send(Ext.util.JSON.encode(formStore));
    }

    function returnchecked() {
        if (httpR_itemInfo.readyState == 4) {
            if (httpR_itemInfo.status == 200) {
                ret = httpR_itemInfo.responseText;
                if (ret == 'ok') {
                    //   Ext.MessageBox.alert('提示', '保存成功！');
                    saveForm();
                } 
                else{
                    Ext.MessageBox.alert("创建失败","因为数据已经存在")

                }
            }
        }
    }
    
    function checkid(){
        var formStore = new Ext.data.Store(['CATANAME','CATANO']);
        formStore.CATANAME = Ext.getCmp("cata_name").getValue();
        formStore.CATANO = Ext.getCmp("cata_no").getValue();
        var saveItemInfoUrl = "";
        saveItemInfoUrl = '?className=cn.com.jsepc.rep.catalog.Catalog_Check';
        saveItemInfoUrl += '&methodName=check';
        saveItemInfoUrl += '&paramCount=1&param_0='+Ext.getCmp("cata_id").getValue();
        saveItemInfoUrl = basePath + 'JarpListener' + saveItemInfoUrl;
        httpR_itemInfo = getXMLHttpRequest();
        httpR_itemInfo.onreadystatechange = returncheckedid;
        httpR_itemInfo.open("POST", saveItemInfoUrl, true);
        httpR_itemInfo.send(Ext.util.JSON.encode(formStore));
    }
    
    function returncheckedid() {
        if (httpR_itemInfo.readyState == 4) {
            if (httpR_itemInfo.status == 200) {
                ret = httpR_itemInfo.responseText;
                if (ret == 'ok') {
                    //   Ext.MessageBox.alert('提示', '保存成功！');
                    updateForm();
                } 
                else{
                    Ext.MessageBox.alert("创建失败","因为数据已经存在")

                }
            }
        }
    }
    
    function check_report(){
        var saveItemInfoUrl = "";
        saveItemInfoUrl = '?className=cn.com.jsepc.rep.catalog.Catalog_Check';
        saveItemInfoUrl += '&methodName=check_report';
        saveItemInfoUrl += '&paramCount=1&param_0='+Ext.getCmp("cata_id").getValue();
        saveItemInfoUrl = basePath + 'JarpListener' + saveItemInfoUrl;
        httpR_itemInfo = getXMLHttpRequest();
        httpR_itemInfo.onreadystatechange = returncheck_report;
        httpR_itemInfo.open("POST", saveItemInfoUrl, true);
        httpR_itemInfo.send();
    }
    
    function returncheck_report() {
        if (httpR_itemInfo.readyState == 4) {
            if (httpR_itemInfo.status == 200) {
                ret = httpR_itemInfo.responseText;
                if (ret == 'ok') {
                    //   Ext.MessageBox.alert('提示', '保存成功！');
                    delete_cata();
                } 
                else{
                    Ext.MessageBox.alert("提示","因为该分类下已有指标，请先取消指标再删除该分类！")

                }
            }
        }
    }
    
    

    function saveForm() {
        var formStore = new Ext.data.Store([ 'CATAID', 'CATANAME',
            'PARENTCATAID', 'CATANO' ]);
        formStore.CATAID = Ext.getCmp("cata_id").getValue();
        formStore.CATANAME = Ext.getCmp("cata_name").getValue();
        formStore.PARENTCATAID = Ext.getCmp("parent_id").getValue();
        formStore.CATANO = Ext.getCmp("cata_no").getValue();
        var saveItemInfoUrl = "";
        saveItemInfoUrl = '?className=cn.com.jsepc.rep.catalog.CatalogDAO';
        saveItemInfoUrl += '&methodName=addCatalog';
        saveItemInfoUrl += '&paramCount=0';
        saveItemInfoUrl = basePath + 'JarpListener' + saveItemInfoUrl;
        httpR_itemInfo = getXMLHttpRequest();
        httpR_itemInfo.onreadystatechange = returnItemSave;
        httpR_itemInfo.open("POST", saveItemInfoUrl, true);
        httpR_itemInfo.send(Ext.util.JSON.encode(formStore));

    }
    function returnItemSave() {
        if (httpR_itemInfo.readyState == 4) {
            if (httpR_itemInfo.status == 200) {
                ret = httpR_itemInfo.responseText;
                if (ret == 'ok') {
                    Ext.MessageBox.alert('提示', '保存成功！');
                    catano=Ext.getCmp("cata_no").getValue();
                    cataname=Ext.getCmp("cata_name").getValue();
                    reload_tree();
                    catano=Ext.getCmp("cata_no").getValue();
                    cataname=Ext.getCmp("cata_name").getValue();
                //  document.getElementById("catalog_tree").reload();
                } 
                else{
                    Ext.MessageBox.alert('警告', '保存失败');
                }
            }
        }
    }
	
    function delete_cata(){
        if(Ext.getCmp("cata_id").getValue()==""){
            Ext.MessageBox.alert("提示","请选中后再删除！");
        }
        else{
        	
            Ext.MessageBox.show({
                title:'是否要删除？',
                msg: ' 是否要删除  '+Ext.getCmp("cata_name").getValue()+' 分类？',
                //  buttons: Ext.MessageBox.YESNOCANCEL,
                buttons:{
                    'yes':'删除',
                    'no':'取消'
                },
                fn: showResult,
                animEl: 'delete_catalog_button',
                icon: Ext.MessageBox.QUESTION
            });
        }
    };
    
    function saveResult(btn){
        if(btn=='yes'){
            updateForm();
        }
        else if(btn=='no'){
		 
        }
    };
    
    function updateForm() {                 //覆盖
        var formStore = new Ext.data.Store([ 'CATAID', 'CATANAME',
            'PARENTCATAID', 'CATANO' ]);
        formStore.CATAID = Ext.getCmp("cata_id").getValue();
        formStore.CATANAME = Ext.getCmp("cata_name").getValue();
        formStore.PARENTCATAID = Ext.getCmp("parent_id").getValue();
        formStore.CATANO = Ext.getCmp("cata_no").getValue();
        var saveItemInfoUrl = "";
        saveItemInfoUrl = '?className=cn.com.jsepc.rep.catalog.CatalogDAO';
        saveItemInfoUrl += '&methodName=updateCatalog';
        saveItemInfoUrl += '&paramCount=0';
        saveItemInfoUrl = basePath + 'JarpListener' + saveItemInfoUrl;
        httpR_itemInfo = getXMLHttpRequest();
        httpR_itemInfo.onreadystatechange = returnItemSave;
        httpR_itemInfo.open("POST", saveItemInfoUrl, true);
        httpR_itemInfo.send(Ext.util.JSON.encode(formStore));

    }
	
    function showResult(btn){
        if(btn=='yes'){
            delete_catalog()
        }
        else if(btn=='no'){
			 
        }
    };

    function delete_catalog() {
        formStore = new Ext.data.Store([ 'CATAID' ]);
        formStore.CATAID = Ext.getCmp("cata_id").getValue();
        var saveItemInfoUrl = "";
        saveItemInfoUrl = '?className=cn.com.jsepc.rep.catalog.CatalogDAO';
        saveItemInfoUrl += '&methodName=deleteCatalog';
        saveItemInfoUrl += '&paramCount=0';
        saveItemInfoUrl = basePath + 'JarpListener' + saveItemInfoUrl;
        httpR_itemInfo = getXMLHttpRequest();
        httpR_itemInfo.onreadystatechange = returndelete_catalog;
        httpR_itemInfo.open("POST", saveItemInfoUrl, true);
        httpR_itemInfo.send(Ext.util.JSON.encode(formStore));

    }
    function returndelete_catalog() {
        if (httpR_itemInfo.readyState == 4) {
            if (httpR_itemInfo.status == 200) {
                ret = httpR_itemInfo.responseText;
                if (ret == 'ok') {
                    Ext.MessageBox.alert('提示', '删除成功！');
                    Ext.getCmp("cata_id").setValue("");
                    Ext.getCmp("cata_name").setValue("");
                    Ext.getCmp("cata_no").setValue("");
                    cataname="";
                    catano="";
                    reload_tree();
                } else {
                    Ext.MessageBox.alert('提示', '删除失败！');
                    
                }
            }
        }
    }
    
    //重新加载树
    function reload_tree(){
               
        tree_root.reload();     
    }
    
    //加载树
    var tree_root = new Ext.tree.AsyncTreeNode({
        text : '指标分类',
        id : '0000'
    });
    var tree = new Ext.tree.TreePanel({
        renderTo:'catalog_tree_div',
        //title: '指标分类',
        border:'0',
        height: 900,
        id:'catalog_tree',
        //    width: 200,
        useArrows:true,     //使用箭头
        autoScroll:true,    //自动滚动
        animate:true,       // 动画
        enableDD:false,      //可拖动
        containerScroll: true,  
        rootVisible: false,
        
        // frame: true,     //添加边框等等
        //loader : new Ext.tree.TreeLoader(),
        root : tree_root,
        autoScroll : true,        
        listeners: {
            beforeload: function(node) {
                //alert('d:'+node.getDepth());
                var openUrl = basePath
                + 'JarpListener?className=cn.com.jsepc.rep.catalog.CatalogDAO&methodName=getCatalog&paramCount=0'
                node.loader = new Ext.tree.TreeLoader({
                    dataUrl : openUrl
                });
              
            }
        }   
    });
    tree.on('click', function(node){
        //        Ext.mytree.resetBoldNodes(tree.getRootNode());
        //        node.getUI().addClass('selitem');
        node=node;
        if(node.id!='root'){
            var b=(cataname!=""&&catano!="")&&(cataname!=Ext.getCmp("cata_name").getValue()||catano!=Ext.getCmp("cata_no").getValue());
            if(b){
                Ext.MessageBox.show({
                    title:'提示',
                    msg: '数据已经更改，但是没有保存，是否要保存？',
                    //  buttons: Ext.MessageBox.YESNOCANCEL,
                    buttons:{
                        'yes':'保存',
                        'no':'丢弃'
                    },
                    fn: save_or_no,
                    animEl: 'delete_catalog_button',
                    icon: Ext.MessageBox.QUESTION
                });
                function save_or_no(btn){
                    if(btn=='yes'){
                        save_button();
                    }
                    else if(btn=='no'){
                        cataname=node.text;
                        catano=node.attributes.cataNO;
                        Ext.getCmp("cata_id").setValue(node.id);
                        Ext.getCmp("cata_name").setValue(node.text);
                        Ext.getCmp("cata_no").setValue(node.attributes.cataNO);
                        Ext.getCmp("parent_id").setValue(node.attributes.parentCataID);	 
                    }
                };
            }
            else{
                cataname=node.text;
                catano=node.attributes.cataNO;
                Ext.getCmp("cata_id").setValue(node.id);
                Ext.getCmp("cata_name").setValue(node.text);
                Ext.getCmp("cata_no").setValue(node.attributes.cataNO);
                Ext.getCmp("parent_id").setValue(node.attributes.parentCataID);
            }
        }
    });
    
    
// tree.getRootNode().expand(true,true);

});